package view;

import search.LeafIndexer;
import search.OffsetSearcher;
import treeview.unit.AbstractUnitView;

import javax.swing.*;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * Created by Pavel Nikolaev
 */
public class HighlightPanel extends JPanel implements PropertyChangeListener {

    private HtmlPanel myHighlightedPanel;
    private LeafIndexer myIndexer;
    private OffsetSearcher mySearcher;
    //private ArrayList<Selection> mySelections;
    private Selection mySelection;
    private Color myColor;

    public HighlightPanel(HtmlPanel highlighted) {
        this.setLayout(new GridLayout(1,1));
        this.add(highlighted);
        myHighlightedPanel = highlighted;
        myIndexer = myHighlightedPanel.getIndexer();
        mySearcher = (myIndexer == null) ? null : new OffsetSearcher(myIndexer);
        //mySelections = new ArrayList<Selection>();
        mySelection = null;
        myColor = Color.BLUE;
    }

    private void highlight(Graphics g, int startOffset, int endOffset, Color color) {
        if (startOffset >= endOffset) return;

        AbstractUnitView startView = myIndexer.getLeafView(mySelection.getStartViewIndex());
        AbstractUnitView endView = myIndexer.getLeafView(mySelection.getEndViewIndex());

        int startIndex = startView.getIndex();
        int endIndex = endView.getIndex();
        if (startIndex == endIndex) {
            startView.highlight(g, startOffset, endOffset, color);
        } else {
            startView.highlight(g, startOffset, startView.getEndOffset(), color);
            for (int i = startIndex + 1; i < endIndex; ++i) {
                myIndexer.getLeafView(i).highlight(g, color);
            }
            endView.highlight(g, endView.getStartOffset(), endOffset, color);
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //if (!myHighlightedPanel.isValid()) myHighlightedPanel.paint(g);
        if (mySelection != null) {
            highlight(g, mySelection.getStartOffset(), mySelection.getEndOffset(), myColor);
        }
        /*
        for (Selection selection : mySelections) {
            highlight(g, selection.getStartOffset(), selection.getEndOffset(), myColor);
        }
        */
    }

    @Override
    public void setSize(int width, int height) {
        myHighlightedPanel.setSize(width, height);
        super.setSize(myHighlightedPanel.getWidth(), myHighlightedPanel.getHeight());
    }

    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals("LeafViewIndexerChanged")) {
            LeafIndexer newIndexer = (LeafIndexer)evt.getNewValue();
            if (myIndexer == null) {
                mySearcher = new OffsetSearcher(newIndexer);
            } else {
                mySearcher.update(newIndexer);
            }
            myIndexer = newIndexer;
        }

        if (evt.getPropertyName().equals("DocumentUpdated")) {
            setSelection(null);
        }
    }

    public void setSelection(Selection selection) {
        mySelection = selection;
        this.revalidate();
        this.repaint();
    }
}

